<?php

namespace App\Http\Controllers\Home;
use App\Model\Good;
use App\Model\Type;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redis;

class HomeController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $buider = Good::query();
        $type = $request->get('type','');
        if (!empty($type)) {
            $buider->where();
        }
        $lists = $buider->orderBy('created_at','desc')->paginate(20);

        return view('home.home', compact('lists'));
    }

    public function create(Request $request,Good $good)
    {
        if ($request->isMethod('get')) {
            $types = Type::where('user_id',Auth::user()['id'])->pluck('name','id');
            return view('home.create',compact('good','types'));
        }
        $data = $request->get('data');

        if (!empty($data['images'][0])) {
            foreach ($data['images'] as $img){
                $data['img'][] = uploadBase64($img)['path'];
            }
        }

        unset($data['images']);

        $data['user_id'] = Auth::user()['id'];

        if (!empty($data['id'])) {
            $model = Good::find($data['id']);
            foreach ($data as $key => $datum) {
                if ($key != 'id') {
                    $good->{$key} = $datum;
                }
            }
//            $good->save();
            info($good->save());
                 return redirect('/home');
//            $model = Good::firstOrCreate(array_only($data,['id']), array_except($data,'id'));
        }else{
            $result = Good::create($data);
            if (!empty($result['id'])) {
                return redirect('/home');
            }
        }






    }

    public function delete($id)
    {
        if (Good::destroy($id)) {
            return redirect('/home');
        }
    }

    public function type()
    {
        $lists = Type::where('user_id',Auth::user()['id'])->orderBy('sort','asc')->paginate(10);
        return view('home.type', compact('lists'));
    }

    public function ctype(Request $request, Type $type)
    {
        if ($request->isMethod('get')) {
            return view('home.ctype',compact('type'));
        }
        $data = $request->get('data');

        $data['user_id'] = Auth::user()['id'];

        $model = Type::firstOrCreate(array_only($data,['name']), array_except($data,'name'));

        if ($model) {
            return redirect('/home/type');
        }
    }

    public function setting(Request $request)
    {
        if ($request->isMethod('get')) {
            $qun = Redis::get('qun');
            $touxiang = Redis::get('touxiang');
            return view('home.setting',compact('qun','touxiang'));
        }
        $aa = $request->get('data');

        Redis::set('qun', $aa['qun']??'');
         Redis::set('touxiang', $aa['touxiang']??'');
        return redirect('/home');

    }
}
